#include<bits/stdc++.h>
#define ll long long
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define Root 1,1,n
#define OK l>=Ll&&r<=Rr
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define pb push_back
#define ull unsigned ll
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned int 
inline ll read(){ll s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57) s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
const int N =1.5e3+5,M=1e6+5,inf=2147000000;
const ll llf=1e18;
const double eps=1e-6;
using namespace std;
int n,m,a[N];
string t;
string s;
struct node{
	int son[10],fail;
	vector<int>end;
}trie[N];
int cnt,ct[N];
inline void insert(string s,int id){
	int len=s.length(),now=0;
	rep(i,0,len-1){
		int k=s[i]-'0';
		if(!trie[now].son[k])trie[now].son[k]=++cnt;
		now=trie[now].son[k];
	}
	trie[now].end.pb(id);
}
inline void getfail(){
	queue<int>q;
	rep(i,0,9)if(trie[0].son[i])q.push(trie[0].son[i]);
	while(!q.empty()){
		int x=q.front();q.pop();
		rep(i,0,9)if(trie[x].son[i])trie[trie[x].son[i]].fail=trie[trie[x].fail].son[i],q.push(trie[x].son[i]);
		else trie[x].son[i]=trie[trie[x].fail].son[i];	
	}
}
string fin,ans;
double ansk;
int b[N],sum;
inline void check(){
	int now=0;
	rep(i,0,n-1){
		int k=fin[i]-'0';
		now=trie[now].son[k];
		for(auto x:trie[now].end)ct[x]++,sum++;
	}
}
inline void baoli(int pos){
	if(pos==n){
		fin=t;
		rep(i,0,n-1)if(t[i]=='.')fin[i]='0'+b[i];
		rep(i,1,m)ct[i]=0;
		sum=0;
		check();
		double now=1.0,p=1.0/(double)sum;
		rep(i,1,m){
			if(!ct[i])continue;
			double k=pow(a[i],p);
			rep(j,1,ct[i])now=now*k;
		}
		if(now-eps>ansk)ans=fin,ansk=now;
		return;
	}
	if(t[pos]!='.'){
		baoli(pos+1);
		return;
	}
	rep(i,0,9)b[pos]=i,baoli(pos+1);
}
int main(){
	freopen("magic.in","r",stdin);
	freopen("magic.out","w",stdout);
	n=read(),m=read();
	cin >>t;
	rep(i,1,m)cin >>s,a[i]=read(),insert(s,i); 
	getfail();
	baoli(0);
	cout<<ans;
	return 0;
}
